//////////////////home sort view/////////////////////
function ajaxSortNewHome(by) {
    var container = zm(".newest_survey").find(".tableContainer");
    if(zm(".newest_survey").find("paging_new").size()===0)
        page=1;
    else
        var page = zm(".newest_survey").find("a.active").text();   
    var tmp = zm(".newest_survey").find(".view-header-new").attr("asc");
    var asc = (tmp === "true") ? false : true;
    zm.ajax({
        method: "POST",
        url: "paging?&page=" + page + "&type=NEW&by=" + by+"&asc=" + asc,
        success: function(data) {
            container.html(data);
            zm(".newest_survey").find(".view-header-new").attr("asc", asc);
            raiseSortEvent();
        }
    });
}


function ajaxSortHotHome(by) {
    var container = zm(".hotest_survey").find(".tableContainer");
    if(zm(".hotest_survey").find(".paging_hot").size()===0)
        page=1;
    else
        var page = zm(".hotest_survey").find("a.active").text();   
    var tmp = zm(".hotest_survey").find(".view-header-hot").attr("asc");
    var asc = (tmp === "true") ? false : true;
    zm.ajax({
        method: "POST",
        url: "paging?&page=" + page + "&type=HOT&by=" + by+"&asc=" + asc,
        success: function(data) {
            container.html(data);
            zm(".hotest_survey").find(".view-header-hot").attr("asc", asc);
            raiseSortEvent();
        }
    });
}
/////////////////////////////////////////////////////



///// delete survey/////
function ajaxDelSurvey() {
    var idSurvey = zm("#survey").attr("idsurvey");
    zm.ajax({
        method: "POST",
        url: "delsur",
        data: {idSurvey: idSurvey},
        success: function(data) {
            //  showPopupSmall(data.error.toString(), data.msg.toString());
            alert(data);

        }
    });
}
//////////////////////





///////////delete response////////

function ajaxDelResponse(idSurvey, arrID) {
    zm.ajax({
        method: "POST",
        url: "delres",
        dataType: 'json',
        data: {idSurvey: idSurvey, arrID: arrID},
        success: function(data) {
            if (data.idUser.length > 0) {

                for (var i = 0; i < data.idUser.length; i++) {
                    zm(".row-table").each(function() {
                        if (parseInt(zm(this).attr("iduser")) === data.idUser[i])
                            zm(this).remove();
                    });
                }
                showPopupSmall("Thông báo", "Đã xóa thành công!");
            }
            else {
                showPopupSmall("Lỗi", "Xóa thất bại!");
            }
        }
    });
}

///////////////////////////////////

//////////////////////home hostes paging////////////////

function  ajaxHostestHome(pageContainer, page) {
    var idUser = 1;
    zm.ajax({
        method: "POST",
        url: "paging?idUser=" + idUser + "&page=" + page + "&type=HOT",
        success: function(data) {
            pageContainer.parent().find(".tableContainer").html(data);

        }
    });
}
///////////////////////////////

///////////////////// response detail paing/////////////

function ajaxResDetail(pageContainer, page) {
    var tableparent = pageContainer.parent().find('.responseContainer');
    var idSurvey = zm('#survey').attr('idsurvey');
    zm.ajax({
        method: "POST",
        url: "responsedetail?idSurvey=" + idSurvey + "&page=" + page,
        success: function(data) {
            tableparent.html(data);
            zm(".namecol-res").unbindAll();
            zm(".namecol-res").bind("click", function() {
                zm("#mudium_popup").remove();
                var value = zm(this).parent().parent().find(".data").html();
                var username = zm(this).text();
                zm(this).parent().parent().parent().find(".view-response").removeClass("active");
                zm(this).addClass("active");
                value = JSON.parse(value);
                showPopupMedium(username, value);
            });
        }
    });


}

/////////////////////////////////////////////////////////


//////////////////home newset paging/////////////////////
function ajaxNewestHome(pageContainer, page) {
    var idUser = 1;
    zm.ajax({
        method: "POST",
        url: "paging?idUser=" + idUser + "&page=" + page + "&type=NEW",
        success: function(data) {
            pageContainer.parent().find(".tableContainer").html(data);
            zm(".view-header").click(function() {
                ajaxSortViewHome();
            });
        }
    });
}
/////////////////////////////////////////////////////

//////////////////////ajax statistic////////////////////////////////
function ajaxStat(pageContainer, page) {
    var url = "";
    var questionContainer = pageContainer.parent();
    var idSurvey = zm('#survey').attr('idsurvey');
    var idQuestion = questionContainer.attr('idquestion');
    url = "answer?idSurvey=" + idSurvey + "&idQuestion=" + idQuestion + "&page=" + page;
    if (pageContainer.parent().find(".table-mutil-line").size() === 1)
        url += "&type=" + multiline;
    zm.ajax({
        method: "POST",
        url: url,
        success: function(data) {
            questionContainer.find(".answer-stats").html(data);
            zm(".namecol-mutil").unbindAll();
            zm(".namecol-mutil").bind("click", function() {
                showContentText(zm(this));
            });
        }
    });
}
///////////////////////////////////////////////////////////////


//////////////add new survey//////////
function ajaxAddSurvey(JSONSurvey) {
    zm.ajax({
        method: "POST",
        url: "add",
        dataType: 'json',
        data: {survey: JSONSurvey},
        success: function(data) {
            showPopupSmall("Thông báo", data.error + ":</br>" + data.msg);
        }
    });
}

///////////////////////////////////

////////////////edit survey//////
function ajaxEditSurvey(idUser, idSurvey, JSONSurvey) {
    zm.ajax({
        type: "POST",
        url: "edited",
        dataType: 'json',
        data: {idUser: idUser, idSurvey: idSurvey, survey: JSONSurvey},
        success: function(data) {
            alert(data.error + data.msg);
        }
    });
}

///////////////////////////////

///////////////submit response///////////
function ajaxSubmitResponse(idSurvey, JSONResponse) {
    zm.ajax({
        method: "POST",
        url: "submit",
        dataType: 'json',
        data: {idSurvey: idSurvey, response: JSONResponse},
        success: function(data) {
            showPopupSmall(data.error.toString(), data.msg.toString());
            
        }
    });
}
///////////////////////////////////////